<meta charset="utf-8">
(#) Using Discouraged Private API

!!! WARNING: Using Discouraged Private API
   This is a warning.

Id
:   `DiscouragedPrivateApi`
Summary
:   Using Discouraged Private API
Severity
:   Warning
Category
:   Correctness
Platform
:   Android
Vendor
:   Android Open Source Project
Feedback
:   https://issuetracker.google.com/issues/new?component=192708
Since
:   3.5.0 (August 2019)
Affects
:   Kotlin and Java files
Editing
:   This check runs on the fly in the IDE editor
See
:   https://developer.android.com/preview/restrictions-non-sdk-interfaces
Implementation
:   [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/PrivateApiDetector.kt)
Tests
:   [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/PrivateApiDetectorTest.kt)

Usage of restricted non-SDK interface may throw an exception at runtime.
Accessing non-SDK methods or fields through reflection has a high
likelihood to break your app between versions, and is being restricted
to facilitate future app compatibility.

(##) Example

Here is an example of lint warnings produced by this check:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text
src/test/pkg/application/ReflectionTestJava.java:11:Warning: Reflective
access to addAssetPath, which is not part of the public SDK and
therefore likely to change in future Android releases
[DiscouragedPrivateApi]
    Method m1 = AssetManager.class.getDeclaredMethod("addAssetPath", String.class);
                ------------------------------------------------------------------
src/test/pkg/application/ReflectionTestJava.java:12:Warning: Reflective
access to addAssetPath, which is not part of the public SDK and
therefore likely to change in future Android releases
[DiscouragedPrivateApi]
    Method m2 = assetManager.getClass().getDeclaredMethod("addAssetPath", String.class);
                -----------------------------------------------------------------------
src/test/pkg/application/ReflectionTestJava.java:13:Warning: Reflective
access to addAssetPath, which is not part of the public SDK and
therefore likely to change in future Android releases
[DiscouragedPrivateApi]
    Method m3 = AssetManager.class.getDeclaredMethod("addAssetPath", path.getClass());
                ---------------------------------------------------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here are the relevant source files:

`src/test/pkg/application/ReflectionTestJava.java`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~java linenumbers
package test.pkg.application;

import android.content.res.AssetManager;

import java.lang.reflect.Method;

public class ReflectionTestJava {
    private static void addAssetPath(AssetManager assetManager) throws Exception {
        String path = "foo/bar";
        Method m1 = AssetManager.class.getDeclaredMethod("addAssetPath", String.class);
        Method m2 = assetManager.getClass().getDeclaredMethod("addAssetPath", String.class);
        Method m3 = AssetManager.class.getDeclaredMethod("addAssetPath", path.getClass());
        Method m4 = AssetManager.class.getDeclaredMethod("invalidateCachesLocked", int.class);
        Method m5 = AssetManager.class.getDeclaredMethod("invalidateCachesLocked", Integer.TYPE);
        Method m6 = AssetManager.class.getDeclaredMethod("invalidateCachesLocked", Integer.class); // OK, doesn't exist

        Class<?> activityClass = Class.forName("android.app.Activity");
        Class<?> bundleClass = Class.forName("android.os.Bundle");
        Method m7 = activityClass.getDeclaredMethod("dispatchActivityPostCreated", bundleClass);
    }
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

`src/test/pkg/application/ReflectionTestKotlin.kt`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~kotlin linenumbers
package test.pkg.application;

import android.content.res.AssetManager

class ReflectionTestKotlin {
    private fun addAssetPath(assetManager: AssetManager) {
        val path = "foo/bar"
        val m1 = AssetManager::class.java.getDeclaredMethod("addAssetPath", String::class.java)
        val m2 = assetManager.javaClass.getDeclaredMethod("invalidateCachesLocked", Int::class.javaPrimitiveType)
        val m3 = assetManager.javaClass.getDeclaredMethod("invalidateCachesLocked", Int::class.java) // OK, doesn't exist
        val m4 = AssetManager::class.java.getDeclaredMethod("addAssetPath", path.javaClass)

        val activityClass = Class.forName("android.app.Activity")
        val bundleClass = Class.forName("android.os.Bundle")
        val m5 = activityClass.getDeclaredMethod("dispatchActivityPostCreated", bundleClass)

    }
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can also visit the
[source code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/PrivateApiDetectorTest.kt)
for the unit tests for this check to see additional scenarios.

The above example was automatically extracted from the first unit test
found for this lint check, `PrivateApiDetector.testJavaReflection`.
To report a problem with this extracted sample, visit
https://issuetracker.google.com/issues/new?component=192708.

(##) Suppressing

You can suppress false positives using one of the following mechanisms:

* Using a suppression annotation like this on the enclosing
  element:

  ```kt
  // Kotlin
  @Suppress("DiscouragedPrivateApi")
  fun method() {
     forName(...)
  }
  ```

  or

  ```java
  // Java
  @SuppressWarnings("DiscouragedPrivateApi")
  void method() {
     forName(...);
  }
  ```

* Using a suppression comment like this on the line above:

  ```kt
  //noinspection DiscouragedPrivateApi
  problematicStatement()
  ```

* Using a special `lint.xml` file in the source tree which turns off
  the check in that folder and any sub folder. A simple file might look
  like this:
  ```xml
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;lint&gt;
      &lt;issue id="DiscouragedPrivateApi" severity="ignore" /&gt;
  &lt;/lint&gt;
  ```
  Instead of `ignore` you can also change the severity here, for
  example from `error` to `warning`. You can find additional
  documentation on how to filter issues by path, regular expression and
  so on
  [here](https://googlesamples.github.io/android-custom-lint-rules/usage/lintxml.md.html).

* In Gradle projects, using the DSL syntax to configure lint. For
  example, you can use something like
  ```gradle
  lintOptions {
      disable 'DiscouragedPrivateApi'
  }
  ```
  In Android projects this should be nested inside an `android { }`
  block.

* For manual invocations of `lint`, using the `--ignore` flag:
  ```
  $ lint --ignore DiscouragedPrivateApi ...`
  ```

* Last, but not least, using baselines, as discussed
  [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).

<!-- Markdeep: --><style class="fallback">body{visibility:hidden;white-space:pre;font-family:monospace}</style><script src="markdeep.min.js" charset="utf-8"></script><script src="https://morgan3d.github.io/markdeep/latest/markdeep.min.js" charset="utf-8"></script><script>window.alreadyProcessedMarkdeep||(document.body.style.visibility="visible")</script>